package com.zcy;

import com.espertech.esper.common.client.EventBean;
import com.espertech.esper.runtime.client.EPEventService;
import com.zcy.event.MyEvent;
import com.zcy.util.EsperBuilder;
import com.zcy.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

/**
 * 14.3.21。时间顺序窗口（time_order或ext:time_order）
 */
@Slf4j
public class TimeOrderWindow extends TimeUtils {

    private static final String _TimeOrderWindow = "@name('time-order-window') select * from MyEvent#time_order(timestamp, 10 sec)";

    /**
     * TODO 没有试验出来
     */
    @Test
    public void sortWinTest() {
        EsperBuilder esperBuilder = new EsperBuilder(_TimeOrderWindow, new Class[]{MyEvent.class});

        esperBuilder.startCompile();

        EPEventService epEventService = esperBuilder.startDeploy((eventBeans, eventBeans1, epStatement, epRuntime) -> {
            for (int i = 0; i < eventBeans.length; i++) {
                EventBean eventBean = eventBeans[i];
                log.info("amount is : {}", eventBean.get("amount"));
            }
        });

        epEventService.sendEventBean(MyEvent.builder().timestamp(1620377232).amount("002").name("001-name").num(10).build(), MyEvent.class.getSimpleName());
        sleep(TimeUnit.SECONDS, 3L);
        epEventService.sendEventBean(MyEvent.builder().timestamp(1000).amount("001").name("001-name").num(20).build(), MyEvent.class.getSimpleName());
        sleep(TimeUnit.SECONDS, 1L);
        epEventService.sendEventBean(MyEvent.builder().timestamp(1620377235).amount("003").name("001-name").num(30).build(), MyEvent.class.getSimpleName());

        sleep(TimeUnit.SECONDS, 10000L);
    }

}
